﻿using System;
using System.Linq;
using System.Text;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;

namespace DataProcessor.Process
{
    public class Downline
    {
        private static StringBuilder sb = new StringBuilder();
        private static readonly ILog Log = LogManager.GetLogger(typeof(Downline));

        //远程反控(如果是水，就采样；如果是其他污染物的就做样)
        public static void Downline3012()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var items = pCtx.反控采样.Where(t => t.执行结果标志ID == 601).ToList();
                    foreach (var item in items)
                    {
                        item.执行结果标志ID = 602;
                        sb.Length = 0;
                        sb.Append("QN=" + item.QN);
                        sb.Append(";ST=32;CN=3012;PW=123456;MN=" + item.企业排口.MN号);
                        sb.Append(";Flag=5;CP=&&PolID=w01018&&");
                        var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                        var now = DateTime.Now;
                        send_ctr_cmd cmd = new send_ctr_cmd
                        {
                            cn = "3012",
                            mn = item.企业排口.MN号,
                            qn = item.QN,
                            is_sent = false,
                            send_string = result,
                            sent_time = now,
                            send_time = now
                        };
                        gCtx.send_ctr_cmd.Add(cmd);
                        Log.Info("远程反控(如果是水，就采样；如果是其他污染物的就做样) " + result);
                    }
                    gCtx.SaveChanges();
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Downline3012Helper()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    var items = pCtx.反控采样.Where(t => t.执行结果标志ID == 602).ToList();
                    foreach (var item in items)
                    {
                        if ((DateTime.Now - item.操作时间).TotalMinutes > 5)
                        {
                            item.执行结果标志ID = 604;
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Downline3022()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var enterprises = pCtx.企业排口.ToList();
                    var items = pCtx.系统参数设置.Where(t => t.参数名称.Contains("取样间隔")).Where(t => t.顺序 == 601).ToList();
                    var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    foreach (var item in items)
                    {
                        item.顺序 = 602;
                        item.参数说明 = qn + ":正在执行";
                        foreach (var enterprise in enterprises)
                        {
                            sb.Length = 0;
                            sb.Append("QN=" + qn);
                            sb.Append(";ST=32;CN=3022;PW=123456;MN=" + enterprise.MN号);
                            sb.Append(";Flag=5;CP=&&SampleInterval=" + item.参数内容 + "&&");
                            var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                            var now = DateTime.Now;
                            send_ctr_cmd cmd = new send_ctr_cmd
                            {
                                cn = "3022",
                                mn = enterprise.MN号,
                                qn = qn,
                                is_sent = false,
                                send_string = result,
                                sent_time = now,
                                send_time = now
                            };
                            gCtx.send_ctr_cmd.Add(cmd);
                            gCtx.SaveChanges();
                            Log.Info("更改做样间隔时间(Sampleinterval>=45,单位：分钟) " + result);
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Downline3097()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var items = pCtx.刷卡及远程充值记录.Where(t => t.充值状态ID == 601).ToList();
                    foreach (var item in items)
                    {
                        item.充值状态ID = 602;
                        sb.Length = 0;
                        sb.Append("QN=" + item.QN);
                        sb.Append(";ST=32;CN=3097;PW=123456;MN=" + item.企业排口.MN号);
                        sb.Append(";Flag=5;CP=&&Gate=ON&&");
                        var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                        var now = DateTime.Now;
                        send_ctr_cmd cmd = new send_ctr_cmd
                        {
                            cn = "3097",
                            mn = item.企业排口.MN号,
                            qn = item.QN,
                            is_sent = false,
                            send_string = result,
                            sent_time = now,
                            send_time = now
                        };
                        gCtx.send_ctr_cmd.Add(cmd);
                        gCtx.SaveChanges();
                        Log.Info("远程控制门禁 " + result);
                    }
                    pCtx.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        //public static void Downline5012()
        //{
        //    try
        //    {
        //        using (var pCtx = new PlatformXpfContext())
        //        using (var gCtx = new GatewayContext())
        //        {
        //            var enterprises = pCtx.企业排口.ToList();
        //            var items = pCtx.IcCards.Where(t => t.Enabled).Where(t => !t.IsSynchronized).ToList();
        //            foreach (var item in items)
        //            {
        //                item.IsSynchronized = true;
        //                foreach (var enterprise in enterprises)
        //                {
        //                    sb.Length = 0;
        //                    var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        //                    sb.Append("QN=" + qn);
        //                    sb.Append(";ST=32;CN=5012;PW=123456;MN=" + enterprise.MN号);
        //                    sb.Append(";Flag=5;CP=&&CarType=" + GetCardType(item.CardType) + ";CardNo=" + item.CardNo + "&&");
        //                    var result = Crc.WrapByCrc(sb.ToString(), "monitor");
        //                    var now = DateTime.Now;
        //                    send_ctr_cmd cmd = new send_ctr_cmd
        //                    {
        //                        cn = "5012",
        //                        mn = enterprise.MN号,
        //                        qn = qn,
        //                        is_sent = false,
        //                        send_string = result,
        //                        sent_time = now,
        //                        send_time = now
        //                    };
        //                    gCtx.send_ctr_cmd.Add(cmd);
        //                    gCtx.SaveChanges();
        //                    Log.Info("开卡下发 " + result);
        //                }
        //            }
        //            gCtx.SaveChanges();
        //            pCtx.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }
        //}

        //public static void Downline5013()
        //{
        //    try
        //    {
        //        using (var pCtx = new PlatformXpfContext())
        //        using (var gCtx = new GatewayContext())
        //        {
        //            var enterprises = pCtx.企业排口.ToList();
        //            var items = pCtx.IcCards.Where(t => !t.Enabled).Where(t => !t.IsSynchronized).ToList();
        //            foreach (var item in items)
        //            {
        //                item.IsSynchronized = true;
        //                foreach (var enterprise in enterprises)
        //                {
        //                    sb.Length = 0;
        //                    var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        //                    sb.Append("QN=" + qn);
        //                    sb.Append(";ST=32;CN=5013;PW=123456;MN=" + enterprise.MN号);
        //                    sb.Append(";Flag=5;CP=&&CarType=" + GetCardType(item.CardType) + ";CardNo=" + item.CardNo + "&&");
        //                    var result = Crc.WrapByCrc(sb.ToString(), "monitor");
        //                    var now = DateTime.Now;
        //                    send_ctr_cmd cmd = new send_ctr_cmd
        //                    {
        //                        cn = "5013",
        //                        mn = enterprise.MN号,
        //                        qn = qn,
        //                        is_sent = false,
        //                        send_string = result,
        //                        sent_time = now,
        //                        send_time = now
        //                    };
        //                    gCtx.send_ctr_cmd.Add(cmd);
        //                    gCtx.SaveChanges();
        //                    Log.Info("销卡下发 " + result);
        //                }

        //            }
        //            gCtx.SaveChanges();
        //            pCtx.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }
        //}
        public static void Downline5012()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var enterprises = pCtx.企业排口.ToList();
                    var items = pCtx.系统参数设置.Where(t => t.参数名称.Contains("卡")).Where(t => t.顺序 == 601).ToList();

                    foreach (var item in items)
                    {
                        item.顺序 = 602;
                        var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        ThreadUtil.Sleep(500);
                        item.参数说明 = qn + ":正在执行";
                        foreach (var enterprise in enterprises)
                        {
                            IcCard icCard = new IcCard
                            {
                                EnterpriseName = enterprise.MN号,
                                EnterpriseId = enterprise.企业基本信息ID,
                                CardNo = item.参数内容,
                                EnableDate = DateTime.Now,
                                CardOwner = "tinz",
                                CardType = item.参数名称,
                                Enabled = true,
                                IsSynchronized = false,
                                Qn = qn
                            };
                            pCtx.IcCards.Add(icCard);
                            pCtx.SaveChanges();

                            sb.Length = 0;
                            sb.Append("QN=" + qn);
                            sb.Append(";ST=32;CN=5012;PW=123456;MN=" + enterprise.MN号);
                            sb.Append(";Flag=5;CP=&&CardType=" + GetCardType(item.参数名称) + ";CardNo=" + item.参数内容 + "&&");
                            var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                            var now = DateTime.Now;
                            send_ctr_cmd cmd = new send_ctr_cmd
                            {
                                cn = "5012",
                                mn = enterprise.MN号,
                                qn = qn,
                                is_sent = false,
                                send_string = result,
                                sent_time = now,
                                send_time = now
                            };
                            gCtx.send_ctr_cmd.Add(cmd);
                            gCtx.SaveChanges();
                            Log.Info("开卡:" + result);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Downline5013()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var enterprises = pCtx.企业排口.ToList();
                    var items = pCtx.系统参数设置.Where(t => t.参数名称.Contains("卡")).Where(t => t.顺序 == 607).ToList();
                    foreach (var item in items)
                    {
                        item.顺序 = 602;
                        var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        ThreadUtil.Sleep(500);
                        item.参数说明 = qn + ":正在执行销卡";
                        foreach (var enterprise in enterprises)
                        {
                            IcCard icCard = new IcCard
                            {
                                EnterpriseName = enterprise.MN号,
                                EnterpriseId = enterprise.企业基本信息ID,
                                CardNo = item.参数内容,
                                EnableDate = DateTime.Now,
                                CardOwner = "tinz",
                                CardType = item.参数名称,
                                Enabled = true,
                                IsSynchronized = false,
                                Qn = qn
                            };
                            pCtx.IcCards.Add(icCard);
                            pCtx.SaveChanges();

                            sb.Length = 0;
                            sb.Append("QN=" + qn);
                            sb.Append(";ST=32;CN=5013;PW=123456;MN=" + enterprise.MN号);
                            sb.Append(";Flag=5;CP=&&CardType=" + GetCardType(item.参数名称) + ";CardNo=" + item.参数内容 + "&&");
                            var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                            var now = DateTime.Now;
                            send_ctr_cmd cmd = new send_ctr_cmd
                            {
                                cn = "5013",
                                mn = enterprise.MN号,
                                qn = qn,
                                is_sent = false,
                                send_string = result,
                                sent_time = now,
                                send_time = now
                            };
                            gCtx.send_ctr_cmd.Add(cmd);
                            gCtx.SaveChanges();

                            Log.Info("消卡:" + result);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Downline5012_2()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var items = pCtx.IcCards.Where(t => t.IsSynchronized!=null&&!t.IsSynchronized.Value && t.Enabled!=null&& t.Enabled.Value).ToList();
                    foreach (var item in items)
                    {
                        sb.Length = 0;
                        sb.Append("QN=" + item.Qn);
                        sb.Append(";ST=32;CN=5012;PW=123456;MN=" + item.EnterpriseName);
                        sb.Append(";Flag=5;CP=&&CardType=" + GetCardType(item.CardType) + ";CardNo=" + item.CardNo + "&&");
                        var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                        var now = DateTime.Now;
                        send_ctr_cmd cmd = new send_ctr_cmd
                        {
                            cn = "5012",
                            mn = item.EnterpriseName,
                            qn = item.Qn,
                            is_sent = false,
                            send_string = result,
                            sent_time = now,
                            send_time = now
                        };
                        gCtx.send_ctr_cmd.Add(cmd);
                        gCtx.SaveChanges();
                        Log.Info("开卡:" + result);
                    }

                    var lastQn = DateTime.Now.AddMinutes(-18).ToString("yyyyMMddHHmmssfff");
                    items = pCtx.IcCards.Where(t => String.Compare(t.Qn, lastQn, StringComparison.Ordinal)<0).Take(1000).ToList();
                    foreach (var item in items)
                    {
                        item.DisableReason = "超时";
                        item.IsSynchronized = true;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Downline5013_2()
        {
            try
            {
                using (var pCtx = new PlatformXpfContext())
                using (var gCtx = new GatewayContext())
                {
                    var items = pCtx.IcCards.Where(t =>t.IsSynchronized!=null&& !t.IsSynchronized.Value && t.Enabled!=null&&!t.Enabled.Value).ToList();
                    foreach (var item in items)
                    {
                        sb.Length = 0;
                        sb.Append("QN=" + item.Qn);
                        sb.Append(";ST=32;CN=5013;PW=123456;MN=" + item.EnterpriseName);
                        sb.Append(";Flag=5;CP=&&CardType=" + GetCardType(item.CardType) + ";CardNo=" + item.CardNo + "&&");
                        var result = Crc.WrapByCrc(sb.ToString(), "monitor");
                        var now = DateTime.Now;
                        send_ctr_cmd cmd = new send_ctr_cmd
                        {
                            cn = "5013",
                            mn = item.EnterpriseName,
                            qn = item.Qn,
                            is_sent = false,
                            send_string = result,
                            sent_time = now,
                            send_time = now
                        };
                        gCtx.send_ctr_cmd.Add(cmd);
                        gCtx.SaveChanges();
                        Log.Info("开卡:" + result);
                    }

                    var lastQn = DateTime.Now.AddMinutes(-18).ToString("yyyyMMddHHmmssfff");
                    items = pCtx.IcCards.Where(t => String.Compare(t.Qn, lastQn, StringComparison.Ordinal) < 0).Take(1000).ToList();
                    foreach (var item in items)
                    {
                        item.DisableReason = "超时";
                        item.IsSynchronized = true;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        private static int GetCardType(string cardType)
        {
            if (cardType.ToUpper().Contains("ADMIN") || cardType.ToUpper().Contains("管理"))
            {
                return 2;
            }
            if (cardType.ToUpper().Contains("MAINTAIN") || cardType.ToUpper().Contains("运维"))
            {
                return 1;
            }
            return 1;

        }
    }
}
